package com.eos.control.action.customer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eos.business.service.SysProductBusinessService;
import com.eos.common.tool.ConstantTool;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.ProductBrand;
import com.eos.orm.mybatis.model.ProductModel;
import com.eos.orm.mybatis.model.SysProduct;
import com.hf.utils.PaginationBean;

public class CategoryListAction extends AbstractBaseAction {
	private static final long serialVersionUID = 628107238108773064L;
	private String brandId;
	private String productId;
	private String dealFlag;
	private String modelName;
	private Integer pageNum;
	private Integer numPerPage;
	private List<ProductModel> modelList;
	private List<SysProduct> productList;
	private List<ProductBrand> brandList;
	private String brandOfProductJson;
	private Integer totalCount;

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getDealFlag() {
		return dealFlag;
	}

	public void setDealFlag(String dealFlag) {
		this.dealFlag = dealFlag;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(Integer numPerPage) {
		this.numPerPage = numPerPage;
	}

	public String getBrandOfProductJson() {
		return brandOfProductJson;
	}

	public List<ProductModel> getModelList() {
		return modelList;
	}

	public List<SysProduct> getProductList() {
		return productList;
	}

	public List<ProductBrand> getBrandList() {
		return brandList;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		try {
			if (pageNum == null || pageNum.intValue() == 0)
				pageNum = 1;
			if (numPerPage == null || numPerPage.intValue() == 0)
				numPerPage = PAGE_SIZE;
			SysProductBusinessService spbs = new SysProductBusinessService();
			Map<String, String> map = new HashMap<String, String>();
			map.put("table_name", "product_model");
			map.put("page_size", numPerPage + "");
			map.put("cur_page", pageNum + "");
			map.put("deal_flag", dealFlag);
			map.put("model_name", modelName);
			map.put("product_id", productId);
			map.put("brand_id", brandId);
			PaginationBean pageBean = spbs.getProductModelList(map);
			totalCount = null != pageBean ? pageBean.getMaxcount() : 0;
			modelList = null != pageBean ? (List<ProductModel>) pageBean
					.getResultList() : null;

			SysProduct sysProduct = new SysProduct();
			sysProduct.setDealFlag(ConstantTool.DEAL_FALG_OK);
			sysProduct.setStatus(ConstantTool.STATUS_OK);
			productList = spbs.getSysProductExample(sysProduct);

			ProductBrand brand = new ProductBrand();
			brand.setStatus(ConstantTool.STATUS_OK);
			brand.setDealFlag(ConstantTool.DEAL_FALG_OK);
			brandList = spbs.getProductBrandExample(brand);

			brandOfProductJson = getProductBrandJson(productList, brandList);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return SUCCESS;
	}

	private String getProductBrandJson(List<SysProduct> proList,
			List<ProductBrand> brandList) {
		if (null != proList && proList.size() > 0 && null != brandList
				&& brandList.size() > 0) {
			Map<String, List<ProductBrand>> map = new HashMap<String, List<ProductBrand>>();
			for (SysProduct pro : proList) {
				for (ProductBrand brand : brandList) {
					int proId = pro.getId();
					if (brand.getProductId() == proId) {
						if (map.containsKey("pro_" + proId)) {
							map.get("pro_" + proId).add(brand);
						} else {
							List<ProductBrand> bList = new ArrayList<ProductBrand>();
							bList.add(brand);
							map.put("pro_" + proId, bList);
						}
					}
				}
			}
			return ObjectUtils.mapToJson(map);
		}
		return "";
	}
}
